/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package ia.perceptron;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import org.neuroph.core.NeuralNetwork;
import org.neuroph.core.learning.SupervisedTrainingElement;
import org.neuroph.core.learning.TrainingSet;
import org.neuroph.nnet.MultiLayerPerceptron;
import org.neuroph.util.TransferFunctionType;

/**
 *
 * @author Igor
 */
public class RedeNeural {

    int numImagens = 200;
    int numPixelsImagens = 2500;
    double[][] entradas = new double[numImagens][numPixelsImagens];
    private NeuralNetwork neuralNetwork;


    public RedeNeural() {
        neuralNetwork = new MultiLayerPerceptron(TransferFunctionType.SIGMOID,2500,36,50,3);
        leArquivo();
        TrainingSet trainingSet = new TrainingSet();
        montaTreinamento(trainingSet);
        //save trained neural network
        //neuralNetwork.save("myMlPerceptron.nnet");

        // load saved neural network
        //neuralNetwork = NeuralNetwork.load("myMlPerceptron.nnet");
        neuralNetwork.learnInSameThread(trainingSet);
      
    }

   private void leArquivo() {
        int count=0;

        try {
            BufferedReader in = new BufferedReader(new FileReader("C:/Documents and Settings/Igor/Meus documentos/NetBeansProjects/MetodoGauss/src/imagensbase/dados.txt"));
            String str;
            while (in.ready()) {
                str = in.readLine();
                montaMatriz(str,count);
                count++;
            }
            in.close();
        }catch (IOException ex) {
            System.out.println("não achou o arquivo");
        }
    }

    private void montaMatriz(String linha, int c) {
        String[] partes = new String[numImagens];
        int num;

        partes = linha.split(" ");
        for (int j=0;j<numPixelsImagens;j++) {
            num = Integer.parseInt(partes[j]);
            entradas[c][j] =num;
        }
    }

    private void montaTreinamento(TrainingSet trainingSet) {
        //treinamento da rede configurando a saída pra cada entrada
        for (int i=0;i<numImagens;i++) {
            entradas[i] = normalizaDados(entradas[i]);
            if (i<40)
                trainingSet.addElement(new SupervisedTrainingElement(entradas[i], new double[]{1,0,0})); //latino
            else if (i>40 && i<80)
                trainingSet.addElement(new SupervisedTrainingElement(entradas[i], new double[]{0,1,1})); //fechada
            else if (i>80 && i<120)
                trainingSet.addElement(new SupervisedTrainingElement(entradas[i], new double[]{1,1,1})); //aberta
          else if (i>120 && i<160)
                trainingSet.addElement(new SupervisedTrainingElement(entradas[i], new double[]{0,0,1})); //aberta invertida
            else
                trainingSet.addElement(new SupervisedTrainingElement(entradas[i], new double[]{0,0,0})); //latino invertida
        }
    }

    private double[] normalizaDados(double[] dados) {
        for (int i=0;i<numPixelsImagens;i++) {
            if (dados[i]<=50) {
                dados[i] = 0;
            } else {
                dados[i] =1;
            }
        }

        return dados;
    }

    public NeuralNetwork getNeuralNetwork() {
        return neuralNetwork;
    }

    public void setNeuralNetwork(NeuralNetwork neuralNetwork) {
        this.neuralNetwork = neuralNetwork;
    }


}
